<template>
  <div class="root">
    <el-card>
      <el-form
        :label-position="labelPosition"
        label-width="100px"
        :model="formLabelAlign"
        style="max-width: 460px"
      >
        <el-form-item label="账号" required>
          <el-input v-model="formLabelAlign.userName" />
        </el-form-item>
        <el-form-item label="密码" required>
          <el-input v-model="formLabelAlign.passWord" type="password" />
        </el-form-item>

        <div style="display: flex; justify-content: space-between">
          <el-button type="primary" @click="clickisRegister">注册</el-button>

          <el-button v-if="!isRegister" type="primary" @click="login"
            >登录</el-button
          >
          <el-button v-if="isRegister" @click="isRegister = false"
            >取消</el-button
          >
        </div>
      </el-form>
    </el-card>
    <el-dialog v-model="showdialog">
      <el-form v-if="showdialog" label-width="100px" style="max-width: 460px">
        <el-form-item label="姓名" required>
          <el-input v-model="RegisterUserData.name" />
        </el-form-item>
        <el-form-item label="账号" required>
          <el-input v-model="RegisterUserData.userName" />
        </el-form-item>
        <el-form-item label="密码" required>
          <el-input v-model="RegisterUserData.passWord" type="password" />
        </el-form-item>

        <div style="display: flex; justify-content: space-between">
          <el-button type="primary" @click="confrimsub">确定</el-button>
          <el-button @click="canfrimsub">取消</el-button>
        </div>
      </el-form>
    </el-dialog>
  </div>
</template>

<script setup>
import { reactive, ref } from "vue";
const labelPosition = ref("right");
import { ElMessage } from "element-plus";
const formLabelAlign = reactive({
  userName: "",
  passWord: "",
});
import { useRouter } from "vue-router";
let $Router = useRouter();
import { useStore } from "vuex";
const store = useStore();
let isRegister = ref(false);
const login = () => {
  if (!formLabelAlign.userName) {
    ElMessage.warning("userName");
    return;
  }
  if (!formLabelAlign.passWord) {
    ElMessage.warning("passWord");
    return;
  }

  //   store.commit("user/setUserInfo", formLabelAlign.name);
  console.log(formLabelAlign, "formLabelAlign");
  let skt = store.commit("user/getFindUser", formLabelAlign);
  console.log(skt, "11");
  console.log(store.state.user.isLogin, "store.state.user.isLogin");
  if (store.state.user.isLogin) {
    $Router.push("/home");
  } else {
    ElMessage.warning("账号或密码错误");
  }
  //   store.commit("user/getFindUser", formLabelAlign);
};
let RegisterUserData = ref({});
let showdialog = ref(false);
let clickisRegister = () => {
  RegisterUserData.value.userName = "";
  RegisterUserData.value.passWord = "";
  showdialog.value = true;
};
let confrimsub = () => {
  if (!RegisterUserData.value.userName) {
    ElMessage.warning("userName");
    return;
  }
  if (!RegisterUserData.value.passWord) {
    ElMessage.warning("passWord");
    return;
  }
  store.commit("user/addUser", RegisterUserData.value);

  $Router.push("/home");
};
let canfrimsub = () => {
  RegisterUserData.value = {};
  showdialog.value = false;
};
</script>

<style lang="scss" scoped>
.root {
  height: 100vh;
  width: 100%;
  display: flex;
  justify-content: center;
  align-items: center;
  background-color: #a0cfff;
}
</style>
